package com.zhoug.chart3demo.chart;

import android.graphics.Color;
import android.util.SparseIntArray;
import android.widget.TextView;

import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.model.GradientColor;
import com.zhoug.chart3.MPChartColors;
import com.zhoug.chart3.MPChartConfig;
import com.zhoug.chart3.charts.MPBarChart;
import com.zhoug.chart3.charts.MPCombinedChart;
import com.zhoug.chart3.charts.MPLineChart;
import com.zhoug.chart3.charts.MPPieChart;
import com.zhoug.chart3.core.MPCombinedKey;
import com.zhoug.chart3.core.MPConstant;
import com.zhoug.chart3.core.MPLineKey;
import com.zhoug.chart3.core.styleconfig.IMPBarStyleConfig;
import com.zhoug.chart3.core.styleconfig.IMPCombinedStyleConfig;
import com.zhoug.chart3.core.styleconfig.IMPLineStyleConfig;
import com.zhoug.chart3.data.HkChartData;
import com.zhoug.chart3.data.MPLegendEntry;
import com.zhoug.chart3.data.OnDateSetCreateListener;
import com.zhoug.chart3.formatters.MPDataSetValueFormatter;
import com.zhoug.chart3.formatters.MPPercentAxisLabelFormatter;
import com.zhoug.chart3.markers.MPCombinedMarker;
import com.zhoug.chart3.markers.MPMarkerData;
import com.zhoug.chart3.utils.MPChartUtils;
import com.zhoug.chart3.views.MPIconView;
import com.zhoug.chart3.views.MPLegendViewGroup;
import com.zhoug.chart3demo.BaseActivity;
import com.zhoug.chart3demo.R;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

/**
 * @Author: zhoug
 * @Date: 2024-02-23
 * @Description:
 */
public class ChartTestActivity extends BaseActivity {
    private static final int[] barColors = {MPChartColors.COLOR_RED, MPChartColors.COLOR_BLUE, MPChartColors.COLOR_GREEN,
            MPChartColors.COLOR_SOIL, MPChartColors.COLOR_DEEP_PURPLE, MPChartColors.COLOR_LAVENDER, MPChartColors.COLOR_GRASS_GREEN,};
    private static final int[] lineColors = {MPChartColors.COLOR_DARK_BLUE, MPChartColors.COLOR_CYAN, MPChartColors.COLOR_LIGHT_BLUE,
            MPChartColors.COLOR_ORANGE, MPChartColors.COLOR_DEEP_PURPLE, MPChartColors.COLOR_MINT_GREEN,};


    private static final List<GradientColor> gradientColors = Arrays.asList(
            new GradientColor(MPChartColors.COLOR_RED, MPChartUtils.setColorAlphaPercent(0.2f, MPChartColors.COLOR_RED)),
            new GradientColor(MPChartColors.COLOR_BLUE, MPChartUtils.setColorAlphaPercent(0.2f, MPChartColors.COLOR_BLUE)),
            new GradientColor(MPChartColors.COLOR_GREEN, MPChartUtils.setColorAlphaPercent(0.2f, MPChartColors.COLOR_GREEN)),
            new GradientColor(MPChartColors.COLOR_SOIL, MPChartUtils.setColorAlphaPercent(0.2f, MPChartColors.COLOR_SOIL)),
            new GradientColor(MPChartColors.COLOR_DEEP_PURPLE, MPChartUtils.setColorAlphaPercent(0.2f, MPChartColors.COLOR_DEEP_PURPLE)),
            new GradientColor(MPChartColors.COLOR_LAVENDER, MPChartUtils.setColorAlphaPercent(0.2f, MPChartColors.COLOR_LAVENDER)),
            new GradientColor(MPChartColors.COLOR_GRASS_GREEN, MPChartUtils.setColorAlphaPercent(0.2f, MPChartColors.COLOR_GRASS_GREEN))
    );

    private static final List<GradientColor> gradColors = Arrays.asList(
            new GradientColor(MPChartColors.COLOR_GREEN, MPChartUtils.setColorAlphaPercent(0.2f, MPChartColors.COLOR_GREEN)),
            new GradientColor(MPChartColors.COLOR_BLUE, MPChartUtils.setColorAlphaPercent(0.2f, MPChartColors.COLOR_BLUE)),
            new GradientColor(MPChartColors.COLOR_YELLOW, MPChartUtils.setColorAlphaPercent(0.2f, MPChartColors.COLOR_YELLOW)),
            new GradientColor(MPChartColors.COLOR_RED, MPChartUtils.setColorAlphaPercent(0.2f, MPChartColors.COLOR_RED))
    );
   private static final int[] gradColorArrays= new int[]{MPChartColors.COLOR_GREEN,MPChartColors.COLOR_BLUE,MPChartColors.COLOR_YELLOW,MPChartColors.COLOR_RED};
    private Random mRandom = new SecureRandom();

    private MPBarChart mBarChart1;
    private MPBarChart mBarChart2;
    private MPBarChart mBarChart3;
    private MPLegendViewGroup mLegendBarChart1;
    private MPLegendViewGroup mLegendBarChart2;
    private MPLegendViewGroup mLegendBarChart3;
    private MPLegendViewGroup mLegendBarChart4;
    private MPBarChart mBarChart4;
    private MPBarChart mBarChart5;
    private MPLegendViewGroup mLegendBarChart5;
    private MPLegendViewGroup mLegendCombinedChart1;
    private MPCombinedChart mCombinedChart1;
    private MPLegendViewGroup mLegendCombinedChart2;
    private MPCombinedChart mCombinedChart2;
    private MPLegendViewGroup mLegendCombinedChart3;
    private MPCombinedChart mCombinedChart3;
    private MPLegendViewGroup mLegendCombinedChart4;
    private MPCombinedChart mCombinedChart4;
    private MPLegendViewGroup mLegendCombinedChart5;
    private MPCombinedChart mCombinedChart5;
    private MPLegendViewGroup mLegendLineChart1;
    private MPLineChart mLineChart1;
    private MPLegendViewGroup mLegendLineChart2;
    private MPLineChart mLineChart2;
    private MPLegendViewGroup mLegendPieChart1;
    private MPPieChart mPieChart1;
    private MPLegendViewGroup mLegendPieChart2;
    private MPPieChart mPieChart2;

    @Override
    protected int getLayoutResId() {
        return R.layout.chart_test_activity;
    }

    @Override
    protected void findViews() {
        mBarChart1 = findViewById(R.id.barChart1);
        mBarChart2 = findViewById(R.id.barChart2);
        mBarChart3 = findViewById(R.id.barChart3);
        mLegendBarChart1 = findViewById(R.id.legendBarChart1);
        mLegendBarChart2 = findViewById(R.id.legendBarChart2);
        mLegendBarChart3 = findViewById(R.id.legendBarChart3);
        mLegendBarChart4 = findViewById(R.id.legendBarChart4);
        mBarChart4 = findViewById(R.id.barChart4);
        mBarChart5 = findViewById(R.id.barChart5);
        mLegendBarChart5 = findViewById(R.id.legendBarChart5);
        mLegendCombinedChart1 = findViewById(R.id.legendCombinedChart1);
        mCombinedChart1 = findViewById(R.id.combinedChart1);
        mLegendCombinedChart2 = findViewById(R.id.legendCombinedChart2);
        mCombinedChart2 = findViewById(R.id.combinedChart2);
        mLegendCombinedChart3 = findViewById(R.id.legendCombinedChart3);
        mCombinedChart3 = findViewById(R.id.combinedChart3);
        mLegendCombinedChart4 = findViewById(R.id.legendCombinedChart4);
        mCombinedChart4 = findViewById(R.id.combinedChart4);
        mLegendCombinedChart5 = findViewById(R.id.legendCombinedChart5);
        mCombinedChart5 = findViewById(R.id.combinedChart5);
        mLegendLineChart1 = findViewById(R.id.legendLineChart1);
        mLineChart1 = findViewById(R.id.lineChart1);
        mLegendLineChart2 = findViewById(R.id.legendLineChart2);
        mLineChart2 = findViewById(R.id.lineChart2);
        mLegendPieChart1 = findViewById(R.id.legendPieChart1);
        mPieChart1 = findViewById(R.id.pieChart1);
        mLegendPieChart2 = findViewById(R.id.legendPieChart2);
        mPieChart2 = findViewById(R.id.pieChart2);
    }

    @Override
    protected void init() {
        initMPBarChart1();
        initMPBarChart2();
        initMPBarChart3();
        initMPBarChart4();
        initMPBarChart5();
        initMPCombinedChart1();
        initMPCombinedChart2();
        initMPCombinedChart3();
        initMPCombinedChart4();
        initMPCombinedChart5();
        initMPLineChart1();
        initMPLineChart2();
    }


    private void initMPBarChart1() {
        MPBarChart chart = mBarChart1;
        MPLegendViewGroup legendViewGroup = mLegendBarChart1;
        chart.getLegend().setEnabled(false);
        IMPBarStyleConfig styleConfig = chart.getStyleConfig();
        styleConfig.setGradientColors(MPChartUtils.copyList(gradientColors, 1));
        chart.setBarTopArcEnable(true);
        MPCombinedMarker marker = new MPCombinedMarker(this);
        marker.setShowType(MPCombinedMarker.SHOW_TYPE_HIGHLIGHT);
        chart.setMarker(marker);
        List<HkChartData> data = new ArrayList<>();
        for (int i = 1; i <= 12; i++) {
            HkChartData hkChartData = new HkChartData();
            hkChartData.setName(i + "月");
            hkChartData.put("违规数量", mRandom.nextInt(1000));
            data.add(hkChartData);
        }
        chart.setHkChartData(data);
        SparseIntArray sparseIntArray = new SparseIntArray();
        sparseIntArray.put(MPConstant.BAR_CHART, MPIconView.RECTANGLE_ARC);
        legendViewGroup.setData(MPChartUtils.computeLegend(chart.getData(), sparseIntArray), true);
    }

    private void initMPBarChart2() {
        MPBarChart chart = mBarChart2;
        MPLegendViewGroup legendViewGroup = mLegendBarChart2;
        chart.getLegend().setEnabled(false);
        IMPBarStyleConfig styleConfig = chart.getStyleConfig();
        styleConfig.setGradientColors(gradColors);
        chart.setBarTopArcEnable(true);
        MPCombinedMarker marker = new MPCombinedMarker(this);
        marker.setShowType(MPCombinedMarker.SHOW_TYPE_GROUP);
        marker.setPercentType(MPCombinedMarker.PERCENT_TYPE_BAR_GROUP);
        marker.showSum(true);
        marker.showPercent(true);
        chart.setMarker(marker);
        List<HkChartData> data = new ArrayList<>();
        for (int i = 1; i <= 12; i++) {
            HkChartData hkChartData = new HkChartData();
            hkChartData.setName(i + "月");
            hkChartData.put("A级", mRandom.nextInt(1000));
            hkChartData.put("B级", mRandom.nextInt(1000));
            hkChartData.put("C级", mRandom.nextInt(1000));
            hkChartData.put("D级", mRandom.nextInt(1000));
            data.add(hkChartData);
        }
        chart.setHkChartData(data);
        SparseIntArray sparseIntArray = new SparseIntArray();
        sparseIntArray.put(MPConstant.BAR_CHART, MPIconView.RECTANGLE_ARC);
        legendViewGroup.setData(MPChartUtils.computeLegend(chart.getData(), sparseIntArray), true);
    }

    private void initMPBarChart3() {
        MPBarChart chart = mBarChart3;
        MPLegendViewGroup legendViewGroup = mLegendBarChart3;
        chart.getLegend().setEnabled(false);
        IMPBarStyleConfig styleConfig = chart.getStyleConfig();
        styleConfig.setBarColors(gradColorArrays);
        styleConfig.setStackBar(true);
        chart.setBarTopArcEnable(true);
        MPCombinedMarker marker = new MPCombinedMarker(this);
        marker.setShowType(MPCombinedMarker.SHOW_TYPE_GROUP);
        marker.setPercentType(MPCombinedMarker.PERCENT_TYPE_BAR_GROUP);
        marker.showSum(true);
        marker.showPercent(true);
        chart.setMarker(marker);
        List<HkChartData> data = new ArrayList<>();
        for (int i = 1; i <= 12; i++) {
            HkChartData hkChartData = new HkChartData();
            hkChartData.setName(i + "月");
            hkChartData.put("A级", mRandom.nextInt(1000));
            hkChartData.put("B级", mRandom.nextInt(1000));
            hkChartData.put("C级", mRandom.nextInt(1000));
            hkChartData.put("D级", mRandom.nextInt(1000));
            data.add(hkChartData);
        }
        chart.setHkChartData(data);
        SparseIntArray sparseIntArray = new SparseIntArray();
        sparseIntArray.put(MPConstant.BAR_CHART, MPIconView.RECTANGLE_ARC);
        legendViewGroup.setData(MPChartUtils.computeLegend(chart.getData(), sparseIntArray), true);
    }

    private void initMPBarChart4() {
        MPBarChart chart = mBarChart4;
        MPLegendViewGroup legendViewGroup = mLegendBarChart4;
        chart.getLegend().setEnabled(false);
        IMPBarStyleConfig styleConfig = chart.getStyleConfig();
      /*  styleConfig.setGradientColors( Arrays.asList(
                new GradientColor(Color.parseColor("#cccccc"), MPChartUtils.setColorAlphaPercent(0.2f, Color.parseColor("#cccccc"))),
                new GradientColor(MPChartColors.COLOR_GREEN, MPChartUtils.setColorAlphaPercent(0.2f, MPChartColors.COLOR_GREEN))
        ));*/
        styleConfig.setBarColors(new int[]{Color.parseColor("#cccccc"),MPChartColors.COLOR_GREEN});
        styleConfig.setStackBar(false);
        styleConfig.setOverlap(true);
        chart.setBarTopArcEnable(true);
        MPCombinedMarker marker = new MPCombinedMarker(this){
            @Override
            protected void onBindItemData(MPIconView mpIcon, TextView tvLabel, TextView tvValue, MPMarkerData.YData data) {
                super.onBindItemData(mpIcon, tvLabel, tvValue, data);
                String label = data.getLabel();
                if("已上传".equals(label)){
                    float groupSum = data.getGroupSum();
                    float value = data.getValue();
                    float allValue=groupSum-value;
                    float ratio=0;
                    if(allValue>0){
                        ratio=value/allValue;
                    }
                    tvValue.setText(MPChartUtils.formatString("%s  (%s%%)", data.getFormatValue(), getPercentValue(ratio)));
                }

            }
        };

        marker.setShowType(MPCombinedMarker.SHOW_TYPE_GROUP);
        marker.showSum(false);
        marker.showPercent(false);
        chart.setMarker(marker);
        List<HkChartData> data = new ArrayList<>();
        for (int i = 1; i <= 12; i++) {
            HkChartData hkChartData = new HkChartData();
            hkChartData.setName(i + "月");
            int all = mRandom.nextInt(100)+10;
            hkChartData.put("应上传", all);
            hkChartData.put("已上传", i == 2 ? all : mRandom.nextInt(all));
            data.add(hkChartData);
        }
        chart.setHkChartData(data);
        SparseIntArray sparseIntArray = new SparseIntArray();
        sparseIntArray.put(MPConstant.BAR_CHART, MPIconView.RECTANGLE_ARC);
        legendViewGroup.setData(MPChartUtils.computeLegend(chart.getData(), sparseIntArray), true);
    }

    private void initMPBarChart5() {
        MPBarChart chart = mBarChart5;
        MPLegendViewGroup legendViewGroup = mLegendBarChart5;
        chart.getLegend().setEnabled(false);
        IMPBarStyleConfig styleConfig = chart.getStyleConfig();
        styleConfig.setBarColors(new int[]{MPChartColors.COLOR_RED});
        styleConfig.setStackBar(false);
        styleConfig.setOneScreen(0);
        chart.setBarTopArcEnable(false);
        MPCombinedMarker marker = new MPCombinedMarker(this);
        marker.setShowType(MPCombinedMarker.SHOW_TYPE_GROUP);
        marker.showSum(false);
        marker.showPercent(false);
        chart.setMarker(marker);
        List<HkChartData> data = new ArrayList<>();
        for (int i = 1; i <= 500; i++) {
            HkChartData hkChartData = new HkChartData();
            hkChartData.setName("区域"+i);
            hkChartData.put("数量", mRandom.nextInt(1000));
            data.add(hkChartData);
        }
        chart.setHkChartData(data);
        SparseIntArray sparseIntArray = new SparseIntArray();
        sparseIntArray.put(MPConstant.BAR_CHART, MPIconView.RECTANGLE_ARC);
        legendViewGroup.setData(MPChartUtils.computeLegend(chart.getData(), sparseIntArray), true);
    }


    private void initMPCombinedChart1() {
        MPCombinedChart chart = mCombinedChart1;
        MPLegendViewGroup legendViewGroup = mLegendCombinedChart1;
        chart.getLegend().setEnabled(false);
        IMPCombinedStyleConfig styleConfig = chart.getStyleConfig();
        styleConfig.setGradientColors(MPChartUtils.copyList(gradColors, 1));
        styleConfig.setLineColors(new int[]{MPChartColors.COLOR_RED});
        chart.setBarTopArcEnable(true);
        MPCombinedMarker marker = new MPCombinedMarker(this);
        marker.setShowType(MPCombinedMarker.SHOW_TYPE_GROUP);
        chart.setMarker(marker);
        List<HkChartData> data = new ArrayList<>();
        for (int i = 1; i <= 12; i++) {
            HkChartData hkChartData = new HkChartData();
            hkChartData.setName(i + "月");
            hkChartData.put("在线", mRandom.nextInt(1000));
            hkChartData.put("在线率", mRandom.nextInt(1000)/1000f);
            data.add(hkChartData);
        }
        chart.setKeys(Arrays.asList(
                new MPCombinedKey("在线",MPConstant.BAR_CHART,MPConstant.LEFT),
                new MPCombinedKey("在线率",MPConstant.LINE_CHART,MPConstant.RIGHT)
        ));
        YAxis axisRight = chart.getAxisRight();
        axisRight.setEnabled(true);
        axisRight.setValueFormatter(new MPPercentAxisLabelFormatter().setMultiply100(true));
        MPChartConfig.setPercentAxis(axisRight, true);
        chart.setLineDataSetValueFormatter(new MPDataSetValueFormatter().setPercent(true).setMultiply100(true));

        chart.setHkChartData(data);
        SparseIntArray sparseIntArray = new SparseIntArray();
        sparseIntArray.put(MPConstant.BAR_CHART, MPIconView.RECTANGLE_ARC);
        legendViewGroup.setData(MPChartUtils.computeLegend(chart.getData(), sparseIntArray), true);
    }

    private void initMPCombinedChart2() {
        MPCombinedChart chart = mCombinedChart2;
        MPLegendViewGroup legendViewGroup = mLegendCombinedChart2;
        chart.getLegend().setEnabled(false);
        IMPCombinedStyleConfig styleConfig = chart.getStyleConfig();
        styleConfig.setGradientColors(MPChartUtils.copyList(gradColors, 1));
        styleConfig.setLineColors(new int[]{MPChartColors.COLOR_BLUE,MPChartColors.COLOR_YELLOW,MPChartColors.COLOR_RED});
        chart.setBarTopArcEnable(true);
        MPCombinedMarker marker = new MPCombinedMarker(this);
        marker.setShowType(MPCombinedMarker.SHOW_TYPE_GROUP);
        chart.setMarker(marker);
        List<HkChartData> data = new ArrayList<>();
        for (int i = 1; i <= 12; i++) {
            HkChartData hkChartData = new HkChartData();
            hkChartData.setName(i + "月");
            hkChartData.put("摄像头总数", mRandom.nextInt(1000)+100);
            hkChartData.put("新建", mRandom.nextInt(100));
            hkChartData.put("停撤", mRandom.nextInt(10));
            hkChartData.put("在线率", mRandom.nextInt(100));
            data.add(hkChartData);
        }
        chart.setKeys(Arrays.asList(
                new MPCombinedKey("摄像头总数",MPConstant.BAR_CHART,MPConstant.LEFT),
                new MPCombinedKey("新建",MPConstant.LINE_CHART,MPConstant.LEFT),
                new MPCombinedKey("停撤",MPConstant.LINE_CHART,MPConstant.LEFT),
                new MPCombinedKey("在线率",MPConstant.LINE_CHART,MPConstant.RIGHT)
        ));
        YAxis axisRight = chart.getAxisRight();
        axisRight.setEnabled(true);
        axisRight.setValueFormatter(new MPPercentAxisLabelFormatter().setMultiply100(false));
        MPChartConfig.setPercentAxis(axisRight, false);
//        chart.setLineDataSetValueFormatter(new MPDataSetValueFormatter().setPercent(true).setMultiply100(false));
        chart.setOnLineDateSetCreateListener(new OnDateSetCreateListener<LineDataSet>() {
            @Override
            public void onInitDataSetStyle(LineDataSet dataSet, int index) {
                dataSet.setDrawFilled(false);
                if(index==2){
                    dataSet.setValueFormatter(new MPDataSetValueFormatter().setPercent(true).setMultiply100(false));
                }
            }
        });
        chart.setHkChartData(data);
        SparseIntArray sparseIntArray = new SparseIntArray();
        sparseIntArray.put(MPConstant.BAR_CHART, MPIconView.RECTANGLE_ARC);
        List<MPLegendEntry> mpLegendEntries = MPChartUtils.computeLegend(chart.getData(), sparseIntArray);
        if(mpLegendEntries!=null && mpLegendEntries.size()>0){
            for (MPLegendEntry item : mpLegendEntries) {
               if("在线率".equals( item.getLabel())){
                   item.setIconShape(MPIconView.LINE_CIRCLE);
               }
            }
        }
        legendViewGroup.setData(mpLegendEntries, true);
    }

    private void initMPCombinedChart3() {
        MPCombinedChart chart = mCombinedChart3;
        MPLegendViewGroup legendViewGroup = mLegendCombinedChart3;
        chart.getLegend().setEnabled(false);
        IMPCombinedStyleConfig styleConfig = chart.getStyleConfig();
        styleConfig.setBarColors(gradColorArrays);
        styleConfig.setLineColors(new int[]{MPChartColors.COLOR_BLUE,MPChartColors.COLOR_YELLOW,MPChartColors.COLOR_RED});
        styleConfig.setStackBar(true);
        chart.setBarTopArcEnable(true);
        MPCombinedMarker marker = new MPCombinedMarker(this);
        marker.setShowType(MPCombinedMarker.SHOW_TYPE_GROUP);
        marker.showSum(true);
        chart.setMarker(marker);
        List<HkChartData> data = new ArrayList<>();
        for (int i = 1; i <= 12; i++) {
            HkChartData hkChartData = new HkChartData();
            hkChartData.setName(i + "月");
            hkChartData.put("小学", mRandom.nextInt(1000)+100);
            hkChartData.put("初中", mRandom.nextInt(1000)+100);
            hkChartData.put("高中", mRandom.nextInt(1000)+100);
            hkChartData.put("新建", mRandom.nextInt(100));
            hkChartData.put("停撤", mRandom.nextInt(10));
            hkChartData.put("在线率", mRandom.nextInt(100)/100f);
            data.add(hkChartData);
        }
        chart.setKeys(Arrays.asList(
                new MPCombinedKey(new String[]{"小学","初中","高中"},MPConstant.BAR_CHART,MPConstant.LEFT),
                new MPCombinedKey("新建",MPConstant.LINE_CHART,MPConstant.LEFT),
                new MPCombinedKey("停撤",MPConstant.LINE_CHART,MPConstant.LEFT),
                new MPCombinedKey("在线率",MPConstant.LINE_CHART,MPConstant.RIGHT)
        ));
        YAxis axisRight = chart.getAxisRight();
        axisRight.setEnabled(true);
        axisRight.setValueFormatter(new MPPercentAxisLabelFormatter().setMultiply100(true));
        MPChartConfig.setPercentAxis(axisRight, true);
//        chart.setLineDataSetValueFormatter(new MPDataSetValueFormatter().setPercent(true).setMultiply100(false));
        chart.setOnLineDateSetCreateListener(new OnDateSetCreateListener<LineDataSet>() {
            @Override
            public void onInitDataSetStyle(LineDataSet dataSet, int index) {
                dataSet.setDrawFilled(false);
                dataSet.setDrawCircles(false);
                if(index==2){
                    dataSet.setValueFormatter(new MPDataSetValueFormatter().setPercent(true).setMultiply100(true));
                    dataSet.setDrawCircles(true);
                }
            }
        });
        chart.setHkChartData(data);
        SparseIntArray sparseIntArray = new SparseIntArray();
        sparseIntArray.put(MPConstant.BAR_CHART, MPIconView.RECTANGLE_ARC);
        List<MPLegendEntry> mpLegendEntries = MPChartUtils.computeLegend(chart.getData(), sparseIntArray);
        if(mpLegendEntries!=null && mpLegendEntries.size()>0){
            for (MPLegendEntry item : mpLegendEntries) {
                if("在线率".equals( item.getLabel())){
                    item.setIconShape(MPIconView.LINE_CIRCLE);
                }
            }
        }
        legendViewGroup.setData(mpLegendEntries, true);
    }

    private void initMPCombinedChart4() {
        MPCombinedChart chart = mCombinedChart4;
        MPLegendViewGroup legendViewGroup = mLegendCombinedChart4;
        chart.getLegend().setEnabled(false);
        IMPCombinedStyleConfig styleConfig = chart.getStyleConfig();
        styleConfig.setGradientColors(MPChartUtils.copyList(gradColors, 3));
        styleConfig.setLineColors(new int[]{MPChartColors.COLOR_BLUE,MPChartColors.COLOR_YELLOW,MPChartColors.COLOR_RED});
        styleConfig.setStackBar(true);
        chart.setBarTopArcEnable(true);
        MPCombinedMarker marker = new MPCombinedMarker(this);
        marker.setShowType(MPCombinedMarker.SHOW_TYPE_GROUP);
        marker.showSum(true);
        chart.setMarker(marker);
        List<HkChartData> data = new ArrayList<>();
        for (int i = 1; i <= 12; i++) {
            HkChartData hkChartData = new HkChartData();
            hkChartData.setName(i + "月");
            hkChartData.put("小学", mRandom.nextInt(1000)+100);
            hkChartData.put("初中", mRandom.nextInt(1000)+100);
            hkChartData.put("高中", mRandom.nextInt(1000)+100);
            hkChartData.put("新建", mRandom.nextInt(100));
            hkChartData.put("停撤", mRandom.nextInt(10));
            hkChartData.put("在线率", mRandom.nextInt(100)/100f);
            data.add(hkChartData);
        }
        chart.setKeys(Arrays.asList(
                new MPCombinedKey("小学",MPConstant.BAR_CHART,MPConstant.LEFT),
                new MPCombinedKey("初中",MPConstant.BAR_CHART,MPConstant.LEFT),
                new MPCombinedKey("高中",MPConstant.BAR_CHART,MPConstant.LEFT),
                new MPCombinedKey("新建",MPConstant.LINE_CHART,MPConstant.LEFT),
                new MPCombinedKey("停撤",MPConstant.LINE_CHART,MPConstant.LEFT),
                new MPCombinedKey("在线率",MPConstant.LINE_CHART,MPConstant.RIGHT)
        ));
        YAxis axisRight = chart.getAxisRight();
        axisRight.setEnabled(true);
        axisRight.setValueFormatter(new MPPercentAxisLabelFormatter().setMultiply100(true));
        MPChartConfig.setPercentAxis(axisRight, true);
//        chart.setLineDataSetValueFormatter(new MPDataSetValueFormatter().setPercent(true).setMultiply100(false));
        chart.setOnLineDateSetCreateListener(new OnDateSetCreateListener<LineDataSet>() {
            @Override
            public void onInitDataSetStyle(LineDataSet dataSet, int index) {
                if(index==2){
                    dataSet.setValueFormatter(new MPDataSetValueFormatter().setPercent(true).setMultiply100(true));
                    dataSet.setDrawCircles(true);
                    dataSet.setDrawFilled(true);
                }else{
                    dataSet.setDrawFilled(false);
                    dataSet.setDrawCircles(false);
                }
            }
        });
        chart.setHkChartData(data);
        SparseIntArray sparseIntArray = new SparseIntArray();
        sparseIntArray.put(MPConstant.BAR_CHART, MPIconView.RECTANGLE_ARC);
        List<MPLegendEntry> mpLegendEntries = MPChartUtils.computeLegend(chart.getData(), sparseIntArray);
        if(mpLegendEntries!=null && mpLegendEntries.size()>0){
            for (MPLegendEntry item : mpLegendEntries) {
                if("在线率".equals( item.getLabel())){
                    item.setIconShape(MPIconView.LINE_CIRCLE);
                }
            }
        }
        legendViewGroup.setData(mpLegendEntries, true);
    }

    private void initMPCombinedChart5() {
        MPCombinedChart chart = mCombinedChart5;
        MPLegendViewGroup legendViewGroup = mLegendCombinedChart5;
        chart.getLegend().setEnabled(false);
        IMPCombinedStyleConfig styleConfig = chart.getStyleConfig();
        styleConfig.setBarColors(new int[]{Color.parseColor("#cccccc"),MPChartColors.COLOR_GREEN});
        styleConfig.setLineColors(new int[]{MPChartColors.COLOR_RED});
        styleConfig.setStackBar(false);
        styleConfig.setOverlap(true);
        chart.setBarTopArcEnable(true);
        MPCombinedMarker marker = new MPCombinedMarker(this);
        marker.setShowType(MPCombinedMarker.SHOW_TYPE_GROUP);
        marker.showSum(false);
        chart.setMarker(marker);
        List<HkChartData> data = new ArrayList<>();
        for (int i = 1; i <= 12; i++) {
            HkChartData hkChartData = new HkChartData();
            hkChartData.setName(i + "月");
            int all = mRandom.nextInt(100);
            if (all == 0) {
                all=80;
            }
            int finish = mRandom.nextInt(all);
            hkChartData.put("应上传", all);
            hkChartData.put("已上传", finish);
            hkChartData.put("上传率", finish*1.0f/all);
            data.add(hkChartData);
        }
        chart.setKeys(Arrays.asList(
                new MPCombinedKey("应上传",MPConstant.BAR_CHART,MPConstant.LEFT),
                new MPCombinedKey("已上传",MPConstant.BAR_CHART,MPConstant.LEFT),
                new MPCombinedKey("上传率",MPConstant.LINE_CHART,MPConstant.RIGHT)
        ));
        YAxis axisRight = chart.getAxisRight();
        axisRight.setEnabled(true);
        axisRight.setValueFormatter(new MPPercentAxisLabelFormatter().setMultiply100(true));
        MPChartConfig.setPercentAxis(axisRight, true);
        chart.setLineDataSetValueFormatter(new MPDataSetValueFormatter().setPercent(true).setMultiply100(true));
        chart.setOnLineDateSetCreateListener(new OnDateSetCreateListener<LineDataSet>() {
            @Override
            public void onInitDataSetStyle(LineDataSet dataSet, int index) {
                dataSet.setDrawValues(true);
            }
        });
        chart.setHkChartData(data);
        SparseIntArray sparseIntArray = new SparseIntArray();
        sparseIntArray.put(MPConstant.BAR_CHART, MPIconView.RECTANGLE_ARC);
        List<MPLegendEntry> mpLegendEntries = MPChartUtils.computeLegend(chart.getData(), sparseIntArray);
        if(mpLegendEntries!=null && mpLegendEntries.size()>0){
            for (MPLegendEntry item : mpLegendEntries) {
                if("在线率".equals( item.getLabel())){
                    item.setIconShape(MPIconView.LINE_CIRCLE);
                }
            }
        }
        legendViewGroup.setData(mpLegendEntries, true);
    }

    private void initMPLineChart1() {
        MPLineChart chart = mLineChart1;
        MPLegendViewGroup legendViewGroup = mLegendLineChart1;
        chart.getLegend().setEnabled(false);
        IMPLineStyleConfig styleConfig = chart.getStyleConfig();
        styleConfig.setLineColors(new int[]{MPChartColors.COLOR_BLUE});
        styleConfig.setLineAttachAxis(false);
        MPCombinedMarker marker = new MPCombinedMarker(this);
        marker.setShowType(MPCombinedMarker.SHOW_TYPE_HIGHLIGHT);
        chart.setMarker(marker);
        List<HkChartData> data = new ArrayList<>();
        for (int i = 1; i <= 12; i++) {
            HkChartData hkChartData = new HkChartData();
            hkChartData.setName(i + "月");
            hkChartData.put("违规数量", mRandom.nextInt(1000));
            data.add(hkChartData);
        }
        chart.setOnDateSetCreateListener(new OnDateSetCreateListener<LineDataSet>() {
            @Override
            public void onInitDataSetStyle(LineDataSet dataSet, int index) {
                dataSet.setDrawValues(true);
                dataSet.setMode(LineDataSet.Mode.LINEAR);
            }
        });
        chart.setHkChartData(data);

        SparseIntArray sparseIntArray = new SparseIntArray();
        sparseIntArray.put(MPConstant.LINE_CHART, MPIconView.LINE_CIRCLE);
        legendViewGroup.setData(MPChartUtils.computeLegend(chart.getData(), sparseIntArray), true);
    }

    private void initMPLineChart2() {
        MPLineChart chart = mLineChart2;
        MPLegendViewGroup legendViewGroup = mLegendLineChart2;
        chart.getLegend().setEnabled(false);
        IMPLineStyleConfig styleConfig = chart.getStyleConfig();
        styleConfig.setLineColors(new int[]{MPChartColors.COLOR_BLUE,MPChartColors.COLOR_RED});
        styleConfig.setLineAttachAxis(true);
        MPCombinedMarker marker = new MPCombinedMarker(this);
        marker.setShowType(MPCombinedMarker.SHOW_TYPE_GROUP);
        chart.setMarker(marker);
        List<HkChartData> data = new ArrayList<>();
        for (int i = 1; i <= 12; i++) {
            HkChartData hkChartData = new HkChartData();
            hkChartData.setName(i + "月");
            hkChartData.put("摄像头数量", mRandom.nextInt(1000));
            hkChartData.put("在线率", mRandom.nextInt(100)/100.0f);
            data.add(hkChartData);
        }
        chart.setKeys(Arrays.asList(
                new MPLineKey("摄像头数量",MPConstant.LEFT),
                new MPLineKey("在线率",MPConstant.RIGHT)
        ));
        YAxis axisRight = chart.getAxisRight();
        axisRight.setEnabled(true);
        axisRight.setValueFormatter(new MPPercentAxisLabelFormatter().setMultiply100(true));
        MPChartConfig.setPercentAxis(axisRight, true);
        chart.setOnDateSetCreateListener(new OnDateSetCreateListener<LineDataSet>() {
            @Override
            public void onInitDataSetStyle(LineDataSet dataSet, int index) {
                dataSet.setDrawFilled(false);
                if(index==1){
                    dataSet.setDrawCircles(true);
                    dataSet.setValueFormatter(new MPDataSetValueFormatter().setPercent(true).setMultiply100(true));
                }else{
                    dataSet.setDrawCircles(false);
                }
            }
        });
        chart.setHkChartData(data);
        SparseIntArray sparseIntArray = new SparseIntArray();
        sparseIntArray.put(MPConstant.LINE_CHART, MPIconView.LINE);
        List<MPLegendEntry> mpLegendEntries = MPChartUtils.computeLegend(chart.getData(), sparseIntArray);
        if(mpLegendEntries!=null && mpLegendEntries.size()>0){
            for (MPLegendEntry item : mpLegendEntries) {
                if("在线率".equals( item.getLabel())){
                    item.setIconShape(MPIconView.LINE_CIRCLE);
                }
            }
        }
        legendViewGroup.setData(mpLegendEntries, true);

    }

}
